package com.java.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.java.dao.PointSignDao;
import com.java.util.CommonUtil;
import com.java.vo.PointSignRecordVo;

/**
 * 商城签到的controller
 * 
 * @author zglc-sc
 */

@Controller
@RequestMapping("/PointSign")
public class QueryPointSignController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    private static final String TOTAL_NUM = "totalNum";

    private static final String PAGE_DATA = "page_data";

    private static void setResponseParams(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET, POST");
        response.setHeader("Content-type", "text/html;charset=UTF-8");
    }

    @Autowired
    private PointSignDao pointSignDao;

    /**
     * 签到记录查询
     * 
     * @param Jdata
     * @param request
     * @param response
     */
    @RequestMapping(value = "/recordList")
    public void signDayRecordList(@RequestParam String Jdata, HttpServletRequest request,
            HttpServletResponse response) {
        try {
            setResponseParams(response);
            com.alibaba.fastjson.JSONObject jsonData = JSON.parseObject(JSON.parseObject(Jdata).get("data").toString());
            Integer userId = jsonData.getInteger("userId");
            Integer page = jsonData.getInteger("page");
            String signTime = jsonData.getString("signTime");
            logger.info("signTime----" + signTime);

            List<PointSignRecordVo> recordList = pointSignDao.findAllSignDayRecord(userId, page, signTime);
            HashMap<String, Object> jsonMap = new HashMap<String, Object>();
            jsonMap.put(TOTAL_NUM, pointSignDao.getSignDayRecordCnt(userId, signTime));
            jsonMap.put(PAGE_DATA, recordList);
            String jsonStr = CommonUtil.toJsonStr(jsonMap);
            logger.info("jsonStr-----", jsonStr.toString());
            response.getWriter().println(jsonStr);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
